<?php
  include_once 'global.php';
  if(isset($_GET['id'])&&$_GET['style']=='diary'&&!empty($_GET['id'])){//判断模板，同时进行日志显示
  	$blogID=intval($_GET['id']);
  	/*******************************************/
  	/*
  	 * 更新阅读次数
  	 */
  	$db->query("UPDATE `myblog` SET  `read_num` = `read_num`+1 WHERE  `blogID`=$blogID;");
  	/*******************************************/
  	$sql="select * from `myblog` where blogID=$blogID;";//读取日志
  	$result=$db->query($sql);
  	$blog=$db->fetch_assoc($result);//将日志赋值到$blog
  	/*******************************************/
  	/*
  	 * 获取日志评论总数
  	 */
  	$sql="select `cmtID` from `comment` where blogID=$blogID;";
  	$result=$db->query($sql);
  	$blog[count_comment]=$db->db_num_rows($result);
  	/*******************************************/
  	/*
  	 * 读取日志评论，进行分页
  	 */
  	$urlnow=$_SERVER[REQUEST_URI];//获取当前url
  	$page=new page($blog[count_comment],$urlnow,3);//新建分页对象Page(总条数,当前url,每页显示条数,页码数)输出变量$pagenav进行分页显示
  	$sql="select * from `comment` where blogID=$blogID order by blogID desc limit $firstcount,$displaypage;";
  	$result=$db->query($sql);
  	$comments=array();
  	while($row=$db->fetch_assoc($result)){
  		$comments[]=$row;
  	}
  	/*******************************************/
  	/*
  	 * 判断是否已经用新浪微博登录，若未登录，则新建对象为登录做准备
  	 */
  	if(!$sinalogined){
  		//$last_url=$_SERVER['REQUEST_URI'];
  		$o = new WeiboOAuth( WB_AKEY , WB_SKEY  );
        $keys = $o->getRequestToken();
        $aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false ,$url);
        //echo $_SESSION['last_url'];
        $_SESSION['keys'] = $keys;
        //echo $aurl;
        unset($o);
        $smarty->assign('aurl',$aurl);
  	}
  	if($_POST['comment']&&$_POST['cmt_user']){//判断是否有新的评论，如果有，则写入数据库
  		//echo $_POST['comment'];
  		$cmt_user=htmlspecialchars($_POST['cmt_user']);
  		$comment=htmlspecialchars($_POST['comment']);
  		$datetime=date("Y-m-d H:i:s",time());
  		$ip=$db->getip();
  		$cmt_pic=$me['profile_image_url'];
  		//echo $cmt_pic;
  		$sql="INSERT INTO `comment` (`blogID`, `cmt_content`, `cmt_time`, `cmt_IP`,`cmt_user`,`cmt_pic`) VALUES ('$blogID', '$comment', '$datetime', '$ip','$cmt_user','$cmt_pic');";
  		$db->query($sql);
  	           if($_POST['updatetoweibo']==true){
  			        $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
  			        $tx="我刚刚在".$url_path.'/blogs.php?style=diary&id='.$blogID.'发表评论：“'.$comment.'”快去看看吧！';
  			        $c->update($tx);
  			        unset($c);
  		        }
  		        echo "<script>alert('评论发表成功！');location.href='blogs.php?style=diary&id=".$blogID."';</script>";
  	}
  	$smarty->assign('blog',$blog);
  	$smarty->assign('pagenav',$pagenav);
  	$smarty->assign('comments',$comments);
  	$smarty->display('diary.html');
  	
  }
  /****************************************************************************************/
  /*
   * 博客列表显示
   */
  if($_GET['style']=='diarylist'){
  	/*
  	 * 读取日志总数
  	 */
  	$sql="select `blogID` from `myblog`;";
  	$result=$db->query($sql);
  	$count_blog=$db->db_num_rows($result);
  	/*
 * 从数据库读取日志,分页显示
 */
  $urlnow=$_SERVER[REQUEST_URI];//获取当前url
  $page=new page($count_blog,$urlnow,8);//新建分页对象Page(总条数,当前url,每页显示条数,页码数)输出变量$pagenav进行分页显示
  $sql="SELECT * FROM `myblog` ORDER BY `blogID` DESC limit $firstcount,$displaypage;";
  $result=$db->query($sql);
  $myblogs=array();
  $blog_ID=array();
  while($row=$db->fetch_assoc($result)){
  	$myblogs[]=$row;
  	$blog_ID[]=$row['blogID'];
  }
  //print_r($myblogs);
/*
 * 从数据库读取日志end
 */ 
  /************************************************************************/
/*
 * 读取日志评论数
 */
  $count_comment=array();
  for($i=0;$i<count($blog_ID);$i++){
  	$sql="select * from `comment` where blogID='$blog_ID[$i]';";
  	$result=$db->query($sql);
  	if($db->db_num_rows($result)){
  	$count_comment[]=$db->db_num_rows($result);}
  	else $count_comment[]=0;
  }
  for($i=0;$i<count($count_comment);$i++){
  	$myblogs[$i][count_comment]="$count_comment[$i]";
  }
  //print_r($myblogs);
/*
 * 读取日志评论数end
 */
  /************************************************************************/
    $smarty->assign('myblogs',$myblogs);
    $smarty->assign('count_blog',$count_blog);
    $smarty->assign('pagenav',$pagenav);
  	$smarty->display('diarylist.html');
  }
  /************************************************************************/
  /*
   * 发表新日志
   */
  if($_GET['style']==writediary){
  	if(!$logined){echo "<script>alert('请先登录');location.href='login.php';</script>";}//判断是否登录，若为登录，转到登录界面
  	else {$smarty->display('writediary.html');
  	if($_POST['blog_title']&&$_POST['blog_content']){
  		$blog_title=$_POST['blog_title'];
  		$blog_content=$_POST['blog_content'];
  		$datetime=date("Y-m-d H:i:s",time());
  		
  		$sql="INSERT INTO `myblog` (`blogID`, `blog_title`, `blog_content`, `blog_pbtime`, `read_num`) VALUES (NULL, '$blog_title', '$blog_content', '$datetime', '0');";
  		if($result=$db->query($sql)){
  			$id=$db->insert_id();
  		    if($_POST['update']==true){
  			   $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
  			   $tx="我刚刚在网站".$url_path.'/blogs.php?style=diary&id='.$id.'发表文章'.$_POST['blog_title'].'快去看看吧！';
  			   $c->update($tx);
  			   unset($c);
  		     }
  			echo "<script>alert('新日志发表成功！');location.href='blogs.php?style=diary&id=".$id."';</script>";
  		}
  	}
  	}
  	
  	//$smarty->display('writediary.html');
  	
  }
  //else {echo "<script>location.href='blogs.php?style=diarylist';</script>";}
    /*
   * 发表新日志end
   */
   /************************************************************************/
  /*
   * 编辑日志
   */
  if($_GET['style']==editdiary&&isset($_GET['edit_id'])&&!empty($_GET['edit_id'])){
  	if(!$logined){echo "<script>alert('请先登录');location.href='login.php';</script>";}//判断是否登录，若为登录，转到登录界面
  	else {$blogID=intval($_GET['edit_id']);
  	/*******************************************/
  	$sql="select * from `myblog` where blogID=$blogID;";//读取日志
  	$result=$db->query($sql);
  	$blog=$db->fetch_assoc($result);//将日志赋值到$blog
  	//print_r($blog);
  	$smarty->assign('blog',$blog);
    $smarty->display('editdiary.html');
  	if($_POST['blog_title']&&$_POST['blog_content']){
  		$blog_title=$_POST['blog_title'];
  		$blog_content=$_POST['blog_content'];
  		$sql="UPDATE `myblog` SET `blog_title`='$blog_title',`blog_content` = '$blog_content'WHERE `blogID` =$blogID;";
  		if($result=$db->query($sql)){
  			echo "<script>alert('编辑成功！');location.href='blogs.php?style=diary&id=".$blogID."';</script>";
  		}
  	}
  	}
  }
  /************************************************************************/
  /*
   * 删除日志
   */
  if(isset($_GET['del_id'])&&!empty($_GET['del_id'])){
  	if(!$logined){echo "<script>alert('请先登录');location.href='login.php';</script>";}//判断是否登录，若为登录，转到登录界面
  	else {$del_id=intval($_GET['del_id']);
  	if($db->query("DELETE FROM `myblog` WHERE `blogID`=$del_id;")&&$db->query("DELETE FROM `comment` WHERE `blogID`=$del_id;")){
  		echo "<script>alert('删除成功！');location.href='blogs.php?style=diarylist';</script>";
  	}
  	}
  }
   //echo "输出".$_SESSION['last_key']['oauth_token'];
?>